﻿using System;
using System.Collections;
using System.Windows;
using MySql.Data.MySqlClient;

namespace ZhuXingFamilys.Database
{
    public class MysqlDba
    {
        static string constring = "server = 8.142.144.242; port=3306; uid = root; pwd = admin123456; database = zxzk; charset=utf8;";   //定义连接mysql字符串
        MySqlConnection sqlCnn = new MySqlConnection(constring);    //连接mysql

        #region 读取Mysql数据库信息
        public void Mysql_read()
        {
            string mobile;
            string cmdstring = "select * from users";    //写入sql
            MySqlCommand sqlCmd = new MySqlCommand(cmdstring, sqlCnn);   //创建命令对象 
            try
            {
                sqlCnn.Open();  //打开数据库
                MySqlDataReader rec = sqlCmd.ExecuteReader();

                while (rec.Read())
                {
                    mobile = rec.GetString(0);
                }

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "error");
            }
            finally
            {
                sqlCnn.Close();
            }
        }
        #endregion

        #region 外部写入Mysql数据库信息
        public void Mysql_insert(string mobile)
        {
            try
            {
                sqlCnn.Close();
                sqlCnn.Dispose();
                string insertstring = "insert into users values('" + mobile + "')";
                MySqlCommand sqlInsert = new MySqlCommand(insertstring, sqlCnn);
                sqlCnn.Open();
                sqlInsert.ExecuteNonQuery();    //插入数据
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "error");
            }
            finally
            {
                sqlCnn.Close();
            }

        }
        #endregion

        #region 
        
        #endregion

    }
}
